$(function () {
    $("#outpay_datagrid").datagrid({
        fit: true,
        fitColumns: true,
        toolbar: '#outpay_toolbar',
        url: '/outpay/list.do',
        singleSelect: true,
        striped: true,
        rownumbers: true,
        pagination: true,
        columns: [[
            {
                title: '投保人', field: 'client' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return value ? value.name: "";
                }
            },
            {
                title: '核保单号', field: 'guard' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return value ? value.num : "";
                }
            },
            {
                title: '需缴金额', field: 'guard1' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.guard.product ? row.guard.product.cost : "";
                }
            },
            {
                title: '缴费状态', field: 'state' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return value ? "<font color='red'>申请缴费</font>" : "<font color='green'>已缴费</font>";
                }
            },
            {
                title: '保险年限/年', field: 'guard2' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.guard.product ? row.guard.product.years : "";
                }
            },
            {
                title: '核保日期', field: 'guard3' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return row.guard ? row.guard.auditDate : "";
                }
            },
            {
                title: '保险机构', field: 'promissgroup' ,align:'center', width: 30, formatter: function (value, row, index) {
                    return value.promise ? value.promise.name : "";
                }
            }
        ]]
    })

    $("#outpay_dialog").dialog({
        width: 360,
        height: 430,
        buttons: '#outpay_btns',
        closed: true,
        border:'thin',
        shadow:true,
        modal:true,
        onClose: function () {
            $("#outpay_form").form("clear")
        }
    })

    //方法点击事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    })

    var methodObj = {

        //高级查询
        doSearch:function () {
            var keyword = $("#keyword").textbox("getValue");
            var num = $("#num").textbox("getValue");
            $("#outpay_datagrid").datagrid("load",{
                keyword:keyword,
                num:num
            })
        },
        //编辑按钮
        edit: function () {
            var data = $("#outpay_datagrid").datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            //各种对象回显
            if (data.client) {
                data['client.name'] = data.client.name;
            }
            if (data.guard) {
                data['guard.product.cost'] = data.guard.product.cost;
            }
            if (data.guard) {
                data['guard.num'] = data.guard.num;
            }
            if (data.promissgroup.promise) {
                data['promissgroup.promise.name'] = data.promissgroup.promise.name;
            }
            if (1) {
                data['product.id'] = data.product.id;
            }
            if (data.promissgroup.promise) {
                data['promissgroup.promise.id'] = data.promissgroup.promise.id;
            }
            if (data.promissgroup.promise) {
                data['guard.employee.id'] = data.guard.employee.id;
            }
            $('#product_datagrid').datagrid({
                url:"/product/selectByPrimaryKey.do?id="+data.product.id,
                fitColumns: true,
                singleSelect: true,
                striped: true,
                scrollbarSize:0,
                columns:[[
                    {title: '险种', field: 'name' ,align:'center', width:10},
                    {title: '险种金额', field: 'cost' ,align:'center', width: 10}
                ]]
            });
            $("#outpay_form").form('load', data);
            $("#outpay_dialog").dialog("open");
            $("#outpay_dialog").dialog('setTitle', '缴费处理');
        },

        //取消按钮
        cancel: function () {
            $("#outpay_dialog").dialog("close");
        },

        // 保存
        save:function(){
            $("#outpay_form").form("submit",{
                url:'/outpay/update.do',
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert('温馨提示','保存成功','info',function () {
                            methodObj.cancel();
                            $("#outpay_datagrid").datagrid("reload");//保持在当前页
                        })
                    }else {
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            })
        }
    }
})

